﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DocManagerORM.ORM;

namespace DocManagerControls.Controls
{
    /// <summary>
    /// Interaction logic for NewTaskPageFunc.xaml
    /// </summary>
    public partial class NewTaskPageFunc : PageFunction<Iters>
    {
        Iters m_Iter = null;
        QueryUsers m_QueryUsers = new QueryUsers();
        QueryCodifiche m_QueryCodifiche = new QueryCodifiche();

        public NewTaskPageFunc()
        {
            InitializeComponent();
            CaricaDatiVari();
        }

        public NewTaskPageFunc(int pIDdoc,string pFileName):this()
        {
            txtIDdoc.Text = pIDdoc.ToString();
            txtFileName.Text = pFileName;
            m_Iter =  Iters.CreateIters(-1,-1,"tit",pIDdoc,Guid.Empty,
                "dest",-1, Guid.Empty,false); 
        }

        public NewTaskPageFunc(Iters pIter,string pFileName):this()
        {
            txtIDdoc.Text = pIter.Fk_IDDoc.ToString();
            txtFileName.Text = pFileName;
            m_Iter = pIter;

            bindaDati();
        }

        private void bindaDati()
        {
            cmbAttIter.SelectedValue = m_Iter.Fk_IDAttivitaIter;
            lstUtenti.SelectedValue = m_Iter.Fk_IDUser_Destinatario;
            txtTitolo.Text = m_Iter.Titolo;
            txtMessaggio.Text = m_Iter.Messaggio;
        }//fM

        private void CaricaDatiVari()
        {
            var attIter = m_QueryCodifiche.getAttivitaIter(1).ToArray();
            var utentiView = m_QueryUsers.getVwMembershipSimple(false).ToArray();
            
            cmbAttIter.DisplayMemberPath = "Attività";
            cmbAttIter.SelectedValuePath = "ID";
            cmbAttIter.ItemsSource = attIter;

            lstUtenti.DisplayMemberPath = "UserName" ;
            lstUtenti.SelectedValuePath = "UserId";
            lstUtenti.ItemsSource = utentiView;

            utentiView = null;
            //aspnet_Roles = null;
        }//fM
        
        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var utenti = lstUtenti.SelectedItem as vwMembershipSimple;
            if (utenti == null)
            { messaggia("Selezionare un destinatario per l'iter"); return; }

            if (lstUtenti.SelectedValue == null)
            {  messaggia("Selezionare un destinatario per l'iter");return;}
            
            if (cmbAttIter.SelectedValue == null)
            { messaggia("Selezionare un'azione per l'iter"); return; }
                        

            m_Iter.Fk_IDUser_Destinatario = Guid.Parse( lstUtenti.SelectedValue.ToString() );
            m_Iter.Destinatario = utenti.UserName;
            m_Iter.Fk_IDAttivitaIter = int.Parse(cmbAttIter.SelectedValue.ToString()) ;
            m_Iter.AttivitaIter  = ((AttivitaIter)cmbAttIter.SelectionBoxItem);
            
            m_Iter.Scadenza = datePicker1.SelectedDate;

            m_Iter.Urgenza =(radio1.IsChecked.Value ? 0: (radio2.IsChecked.Value ? 1:2 )  ) ;
 
            m_Iter.Messaggio = txtMessaggio.Text;
            m_Iter.Titolo = txtTitolo.Text;
                                    
            OnReturn(new ReturnEventArgs<Iters>(m_Iter));
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Cancel task
            OnReturn(null);
        }

        private void txtIDdoc_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !AreAllValidNumericChars(e.Text);
            base.OnPreviewTextInput(e);

        }//fM

        private bool AreAllValidNumericChars(string str)
        {
            foreach (char c in str)
            {
                if (!Char.IsNumber(c)) return false;
            }

            return true;
        }//fM

        protected void messaggia(string pMessage)
        {  
            MessageBox.Show(pMessage, "Avvisi", MessageBoxButton.OK, MessageBoxImage.Information);
        }//fM


    }//fC
}
